package com.lsh.radardemo;

import android.Manifest;
import android.content.Intent;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.util.Range;
import android.view.TextureView;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout;

import java.util.Arrays;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "USBCamera";
    private static final int REQUEST_CAMERA_PERMISSION = 1;
    private TextView tvLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 创建一个垂直布局
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        // 顶部添加返回按钮
        Button backButton = new Button(this);
        backButton.setText("返回");
        backButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        });
        layout.addView(backButton);

        // 添加显示定位信息的 TextView
        tvLocation = new TextView(this);
        tvLocation.setText("正在获取位置信息...");
        layout.addView(tvLocation);

        setContentView(R.layout.activity_main);

        Button btnRadar = findViewById(R.id.btn_radar);
        if (btnRadar != null) {
            btnRadar.setOnClickListener(v -> {
                startActivity(new Intent(MainActivity.this, RadarDisplayActivity.class));
            });
        }

        Button btnGyroscope = findViewById(R.id.btn_gyroscope);
        if (btnGyroscope != null) {
            btnGyroscope.setOnClickListener(v -> {
                startActivity(new Intent(MainActivity.this, Senser.class));
            });
        }

        Button btnBattery = findViewById(R.id.btn_battery);
        if (btnBattery != null) {
            btnBattery.setOnClickListener(v -> {
                startActivity(new Intent(MainActivity.this, VehicleBatteryActivity.class));
            });
        }

        Button btnVideo = findViewById(R.id.btn_video);
        if (btnVideo != null) {
            btnVideo.setOnClickListener(v -> {
                startActivity(new Intent(MainActivity.this, CameraPreviewActivity.class));
            });
        }

        Button btnVehicleLocation = findViewById(R.id.btn_vehicle_location);
        if (btnVehicleLocation != null) {
            btnVehicleLocation.setOnClickListener(v -> {
                startActivity(new Intent(MainActivity.this, VehicleLocationActivity.class));
            });
        }
    }

    // 新增权限请求结果处理
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限已授予，重新尝试打开摄像头
                // The textureView is removed, so this logic is no longer applicable.
                // If the intent was to re-open the camera, it would need to be re-added.
            } else {
                finishAffinity();
                System.exit(0);
//                Toast.makeText(this, "需要摄像头权限才能使用此功能", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
